<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>请假申请</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>请假申请</h1>
<form id="leaveForm">
    <label for="reason">请假原因:</label>
    <textarea id="reason" name="reason" required></textarea><br><br>

    <label for="leaveBegin">请假开始时间:</label>
    <input type="datetime-local" id="leaveBegin" name="leaveBegin" required><br><br>

    <label for="leaveEnd">请假结束时间:</label>
    <input type="datetime-local" id="leaveEnd" name="leaveEnd" required><br><br>

    <input type="hidden" id="userId" name="userId" value="123456"> <!-- 假设当前登录用户的ID为123456 -->
    <button type="submit">提交请假申请</button>
</form>

<script>
    $(document).ready(function() {
        $('#leaveForm').on('submit', function(event) {
            event.preventDefault();

            const userLeaveDTO = {
                userId: $('#userId').val(),
                reason: $('#reason').val(),
                leaveBegin: $('#leaveBegin').val(),
                leaveEnd: $('#leaveEnd').val()
            };

            $.ajax({
                url: '/leave',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(userLeaveDTO),
                success: function(response) {
                    alert('请假申请成功');
                    window.location.href = '/main'; // 跳转到主页面
                },
                error: function(error) {
                    alert('请假申请失败: ' + error.responseText);
                }
            });
        });
    });
</script>
</body>
</html>
